import {RepositoryNodeInputVariables} from "@/view/graph/model/type";

export function isConfirm(rule: any, value: string, callback: any) {
  if (value === '确认') {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function leveTwoSceneNameValidate(rule: any, value: string, callback: any) {
  const pattern = /^(?![0-9])[a-zA-Z0-9-\u4e00-\u9fa5]{1,20}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function treeAddVal(rule: any, value: string, callback: any) {
  const pattern = /^[\u4e00-\u9fa5a-zA-Z0-9_]{1,10}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

/**
 * 标签名称中英文50字符以内
 */
export function labelNameVal(rule: any, value: string, callback: any) {
  const pattern = /^[\u4e00-\u9fa5a-zA-Z]{1,50}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

/**
 * 标签英文名称英文50字符以内
 */
export function labelEnglishNameVal(rule: any, value: string, callback: any) {
  const pattern = /^[a-zA-Z_]{0,50}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function serviceNameVal(rule: any, value: string, callback: any) {
  const pattern = /^[\u4E00-\u9FA5a-zA-Z][\u4E00-\u9FA5a-zA-Z0-9]{0,49}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

/**
 * 校验正整数
 */
export function validatePositiveInteger(rule: any, value: string, callback: any) {
  const pattern = /^[1-9]\d*$/;
  if (pattern.test(value) || !value) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function isValidNumber(rule: any, value: string, callback: any) {
  const pattern = /^(100|[1-9]?[0-9])$/;
  if (pattern.test(value) || !value) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function isNaturalNumber(rule: any, value: string, callback: any) {
  const pattern = /^[1-9]$|^10$/;
  if (pattern.test(value) || !value) {
    //校验通过
    callback();
  } else {
    callback(new Error(rule.message));
  }
}

export function ValidateNumber(rule: any, value: string, callback: any) {
  const pattern = /^(0|[1-9][0-9]?|100)(\.[0-9]{1,2})?$/;
  if (pattern.test(value) || !value) {
    //校验通过
    callback();
  } else {
    callback('请输入小于100的正数或者两个小数');
  }
}

export function ValidateVersion(rule: any, value: string, callback: any) {
  const pattern = /^V(\d{1,2})\.(\d{1,2})\.(\d{1,2})$/;
  if (pattern.test(value) || !value) {
    //校验通过
    callback();
  } else {
    callback('模型版本格式为V数字.数字.数字，数字不可超过两位');
  }
}

export function IsValidOneNumber(rule: any, value: string, callback: any) {
  const pattern = /^-?\d+(\.\d+)?$/;
  if (!rule.required) {
    //校验通过
    callback();
  }
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback('请输入数字类型的值');
  }
}

export function IsValidIdentifier(rule: any, value: string, callback: any) {
  const pattern = /^[a-zA-Z][a-zA-Z0-9_]{0,99}$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback('只能包含英文字母,数字及下划线,并以英文字母开头1-100字符');
  }
}

export function IsValidInputNumber(rule: any, value: string, callback: any) {
  const pattern = /^(?:[1-9][0-9]{0,4}|10000[0-9]{0,1}|100000)$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback('请输入1-100000的正整数');
  }
}

export function isValidString(rule: any, value: string, callback: any) {
  const pattern = /^(?!.*\..*)(.{1,20})$/;
  if (pattern.test(value)) {
    //校验通过
    callback();
  } else {
    callback('请输入不含有.的1-20的字符');
  }
}

export function RepeatValidator(rule: any, value: any, callback: any) {
  const { variableItem, input_variables, message1 } = rule;
  if (value) {
    const list = input_variables.filter(
      (item: RepositoryNodeInputVariables) => item.uuId !== variableItem.uuId
    );
    const isVariable = list.some(
      (item: RepositoryNodeInputVariables) => item.variable === variableItem.variable
    );
    if (isVariable) {
      callback(`${message1}不可重复`);
    } else {
      if (value.length > 100) {
        callback(`${message1}为1-100个字符`);
      } else if (value.includes('.')) {
        callback(`${message1}不能含有符号'.'`);
      } else {
        callback();
      }
    }
  } else {
    callback(`请输入${message1}`);
  }
}

export function ValidatorNodeTitle(rule: any, value: any, callback: any) {
  if (value) {
    const { title, getNode } = rule;
    const titleList = getNode?.model?.graph
      .getNodes()
      ?.filter((item: any) => item.id !== getNode.id)
      ?.map((item: any) => item.getData().title);
    const isTitle = titleList?.some((item: string) => item === title);
    if (isTitle) {
      callback('标题不可重复');
    } else {
      if (title.length > 100) {
        callback('标题长度不可超过100字符');
      } else {
        callback();
      }
    }
  } else {
    callback('请输入标题');
  }
}
export function outputValidator(rule: any, value: any, callback: any) {
  const { variableItem, out_variables, message1 } = rule;
  if (variableItem.data.variable) {
    const parent = variableItem.parent;
    const schema: any[] = parent.data.schema || parent.data;
    const list = schema.filter(item => item.id !== variableItem.data.id);
    const isVariable = list.some((item: any) => item.variable === variableItem.data.variable);
    if (isVariable) {
      callback(`${message1}不可重复`);
    } else {
      if (variableItem.data.variable.length > 100) {
        callback(`${message1}为1-100个字符`);
      } else if (variableItem.data.variable.includes('.')) {
        callback(`${message1}不能含有符号'.'`);
      } else {
        callback();
      }
    }
  } else {
    callback(`请输入${message1}`);
  }
}

export function ParameterInputValidator(rule: any, value: any, callback: any) {
  const { variableItem, input_variables, message1 } = rule;
  if (value) {
    const list = input_variables.filter(
      (item: RepositoryNodeInputVariables) => item.id !== variableItem.id
    );
    const isVariable = list.some(
      (item: RepositoryNodeInputVariables) => item.variable === variableItem.variable
    );
    if (isVariable) {
      callback(`${message1}不可重复`);
    } else {
      if (value.length > 100) {
        callback(`${message1}为1-100个字符`);
      } else if (value.includes('.')) {
        callback(`${message1}不能含有符号'.'`);
      } else {
        callback();
      }
    }
  } else {
    callback(`请输入${message1}`);
  }
}

export function ValidateNumberRegex(rule: any, value: any, callback: any) {
  const regex = /^([1-9]|10)$/;
  if (regex.test(value.toString())) {
    callback(); // 校验通过
  } else {
    callback('请输入1-10的正整数');
  }
}

export function ValidateBestNumberRegex(rule: any, value: any, callback: any) {
  const regex = /^([1-9]\d{0,3}|10000)$/;
  if (regex.test(value.toString())) {
    callback(); // 校验通过
  } else {
    callback('请输入1-10000的正整数');
  }
}

export function isValidEnName(rule: any, value: any, callback: any) {
  const regex = /^[A-Za-z][A-Za-z0-9_]*$/;
  if (regex.test(value)) {
    callback(); // 校验通过
  } else {
    callback('请输入只能包含英文、数字、_，并且只能以英文开头');
  }
}
